package com.andnnl.mock.actions;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * Created by chenss on 2020/10/28.
 */
@Controller
public class IndexController {
    static Logger logger = LoggerFactory.getLogger(IndexController.class);
    public static List<String> list = new LinkedList<>();
    static DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @RequestMapping({"/", "/index"})
    public String index() {
        return "index";
    }

    /**
     * callback方法
     *
     * @param name
     * @return
     */
    @RequestMapping("/cb/{name}")
    @ResponseBody
    public String index(@PathVariable String name, @RequestBody String body) {
//        logger.info("/cb");
        LocalDateTime date3 = LocalDateTime.now();//代替calendar
        String s = dtf.format(date3) +"【"+name+"】"+ " " + body;
        logger.info(s);
        list.add(s);
        return "success";
    }

    @RequestMapping("/datalist")
    @ResponseBody
    public Map datalist() {
        logger.info("/datalist");
        Map<String, Object> map = new HashMap<>();
        map.put("data", list);
        return map;
    }

    @RequestMapping("/testlist")
    @ResponseBody
    public Map testlist() {
        logger.info("/testlist");
        Map<String, Object> map = new HashMap<>();
        LocalDateTime date3 = LocalDateTime.now();//代替calendar
        String s = dtf.format(date3);
        list.add(s+" test 1");
        list.add(s+" test 2");
        list.add(s+" test 3");
        map.put("data", list);
        return map;
    }

    @RequestMapping("/clear")
    @ResponseBody
    public String clear() {
        logger.info("/clear");
        list.clear();
        return "success";
    }

}
